//获取应用实例
const app = getApp();
var util = require('../../../utils/util.js');
var time, goBack, _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userBrandsModel: '',
    userBrandModel:'',
    brandAll: '',
    noBrand: false,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    if (options.time){
      time = options.time
    }else{
      time = 1;
    }
    if (options.goBack){
      goBack = options.goBack
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '加载中',
    })
    _this.setData({
      userBrandsModel: app.globalData.xieYouUserInfoRoot.userBrandsModel,
      noBrand: true
    })

    // 取消加载中
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  //更改用户默认绑定机型
  changeUserBindBrandModel(e){
    var userBrandModelId = '';
    if (e.target.dataset.modelId){
      userBrandModelId = e.target.dataset.modelId;
    }else{
      userBrandModelId = e.currentTarget.dataset.modelId;
    }
    var userCode = _this.data.userBrandsModel[0].userCode;
    //获取品牌
    app.changeUserBindBrandModel(userCode, userBrandModelId, function (data) {
      if (data == 'SUCCESS') {
        _this.initUserBrandModelFun(userCode,function(){
          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1];   //当前页面
          var prevPage = pages[pages.length - 1 - time];  //上一个页面
          var indexPage = pages[0];  //首页
          if (goBack == 'index') {
            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
            indexPage.setData({
              userBrandModel: _this.data.userBrandModel,
              brandLogo: _this.data.userBrandModel.brand.brandLogo,
              brandModelName: _this.data.userBrandModel.brandModel.name
            })
            if (!_this.data.buttonClicked) {
              util.buttonClicked(_this);
              wx.navigateBack({
                delta: pages.length - 1
              })
            }
          } else if (goBack == 'recommend') {
            prevPage.setData({
              brandLogo: _this.data.userBrandModel.brand.brandLogo,
              brandModel: _this.data.userBrandModel.brandModel
            })
            prevPage.getRecommendedProductsList();
            var prevPageThree = pages[pages.length - 2 - time];
            prevPageThree.setData({
              userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
              brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
              brandModel: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel
            })
            if (!_this.data.buttonClicked) {
              util.buttonClicked(_this);
              wx.navigateBack({
                delta: pages.length - 2
              })
            }
          }
          
        })
        
      }
    });
  },
  // 删除绑定机型
  productDelete(e){
    console.log(e);
    var userCode = _this.data.userBrandsModel[0].userCode;
    // 获取机型编码
    var userBrandModelId = e.target.dataset.modelId ? e.target.dataset.modelId : e.currentTarget.dataset.modelId;
    //是否是签约机型
    var isRegisterDevice = e.target.dataset.isRegisterDevice ? e.target.dataset.isRegisterDevice : e.currentTarget.dataset.isRegisterDevice;
    if (isRegisterDevice){
      alter('不可以删除签约机型')
    }else{
      //删除绑定机型
      app.productDelete(userCode, userBrandModelId, function (data) {
        if (data.status == 'success') {
          _this.initUserBrandModelFun(userCode,function(){
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 1 - time];  //上一个页面
            if (goBack == 'index') {
              //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
              if (_this.data.userBrandModel){
                prevPage.setData({
                  userBrandModel: _this.data.userBrandModel,
                  brandLogo: _this.data.userBrandModel.brand.brandLogo,
                  brandModelName: _this.data.userBrandModel.brandModel.name
                })
              }else{
                prevPage.setData({
                  userBrandModel: _this.data.userBrandModel,
                  brandLogo: '',
                  brandModelName: ''
                })
              }
              
            } else if (goBack == 'recommend') {
              if (_this.data.userBrandModel) {
                prevPage.setData({
                  brandLogo: _this.data.userBrandModel.brand.brandLogo,
                  brandModel: _this.data.userBrandModel.brandModel.name
                })
              } else {
                prevPage.setData({
                  brandLogo: '',
                  brandModel: ''
                })
              }
            }
          });
        }
      });
    }
  },
  // 修改绑定机型数据
  initUserBrandModelFun(code,callback) {
    if (typeof code == 'string') {
      var userCode = code
    } else {
      var userCode = code.code
    }
    app.globalData.xieYouUserInfoRoot.userBrandsModel = [];
    app.globalData.xieYouUserInfoRoot.userBrandModel = null;
    app.initUserBrandModel(userCode, function (data) {
      console.log('app初始化绑定机型');
      var dataList = data;
      for (var i = 0; i < data.length; i++) {
        //将用户机型数据存入到全局数组
        app.globalData.xieYouUserInfoRoot.userBrandsModel.push(data[i]);
        // 将选中的机型显示到选择框中
        if (data[i].selected == 1) {
          console.log(data[i]);
          app.globalData.xieYouUserInfoRoot.userBrandModel = data[i]
        }
      }
      _this.setData({
        userBrandsModel: app.globalData.xieYouUserInfoRoot.userBrandsModel,
        userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel
      })
      if (callback){
        callback();
      }
      
    });
  },
  // 增加绑定机型
  productAdd(){
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        title: 'goback',
        url: '/pages/index/user-model/all-brand/all-brand?goBack=' + goBack
      })
    }
  }
})

